package bairui.international.weixin.portal.miniapp.controller.product.product;

import java.util.List;
import java.util.stream.Collectors;

import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.model.domain.jpa.TB_ProductPropertyItem;
import bairui.international.service.Service_ProductPropertyItem;
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
public class Output { 
	@Getter private Long rootCategoryId;	
	@Getter private String rootCategoryName;	
	@Getter private Long id;
	@Getter private String name;
	@Getter private String comment;
	@Getter private Double price;
	@Getter private Double showPrice;
	@Getter private Double score;
	@Getter private String imgUrl;
	@Getter private List<OutputP> properties;
	@Getter private Boolean selected;
	@Getter private Long townId;
	@Getter private Double totalScore;	// 可易物总积分
	@Getter private String unit = "";
	
	public Output(TB_Product it, Double totalScore, Long townId, Service_ProductPropertyItem service_ProductPropertyItem){
		List<TB_ProductPropertyItem> pItem = service_ProductPropertyItem.list(it2->it2.products.any().eq(it));
		Boolean selected = null;
		if(null != townId){
//			if(service_ProxyArea.exists(new ID_ProxyArea(it, new TB_Town(townId)))){
//				selected = true;
//			}else{
				selected = false;
//			}
		}
		//
		this.rootCategoryId = it.getRootCategory().getId();
		this.rootCategoryName = it.getRootCategory().getName();
		this.id = it.getId();
		this.name = it.getName();
		this.price = it.getPrice();
		this.showPrice = it.getShowPrice();
		this.score = it.getScore();
		this.comment = null == it.getComment() ? "" : it.getComment();
		this.imgUrl = new C_Product_ShowImg_Get.Get.Operate() {
			@Override public Object getId() {
				return it.getId();
			}
		}.getUrl();
		this.properties = pItem
			.stream()
			.map(it2->new OutputP(it2.getId(), it2.getName()))
			.collect(Collectors.toList());
		this.selected = selected;
		this.townId = townId;
		this.totalScore = totalScore;
		//
		for(TB_ProductPropertyItem itemP : pItem){
			if(itemP.getProductProperty().getName().trim().equals("单位")){
				this.unit = itemP.getName();
				break;
			}
		}
	}
	
	@AllArgsConstructor
	private static class OutputP { 
		@Getter private Long id;
		@Getter private String name;
	}
}